home *** CD-ROM | disk | FTP | other *** search
/ IRIX 6.3 Development Libraries / SGI IRIX 6.3 Development Libraries.iso / dist6.3 / gl_dev.idb / usr / share / src / OpenGL / teach / glc / test2.c.z / test2.c
Encoding:
C/C++ Source or Header  |  1996-12-06  |  9.9 KB  |  435 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <math.h>
  5. #include <GL/gl.h>
  6. #include <GL/glc.h>
  7. #include <GL/glut.h>
  8.  
  9.  
  10. GLuint contextID;
  11. GLuint fontID, fontIDs[100];
  12. GLboolean dl = GL_FALSE;
  13. GLenum drawStyle = GLC_BITMAP;
  14. GLint faceIndex, faceTotal, facesIndex[100], facesTotal[100];
  15. GLfloat rotX = 0.0, rotY = 0.0, rotZ = 0.0, scale = 30.0;
  16. GLint fontIndex, totalFont;
  17. GLint charIndex;
  18.  
  19.  
  20. void Init(void)
  21. {
  22.     int i, index;
  23.     char buf[80];
  24.     const char *ptr;
  25.  
  26.     if ((contextID = glcGenContext()) == 0) {
  27.     printf("Died at glcGenContext\n");
  28.     exit(1);
  29.     }
  30.  
  31.     if (glcIsContext(contextID) == GL_FALSE) {
  32.     printf("Died at glcIsContext\n");
  33.     exit(1);
  34.     }
  35.  
  36.     glcContext(contextID);
  37.     if (glcGetError() != GL_NO_ERROR) {
  38.     printf("Died at glcContext\n");
  39.     exit(1);
  40.     }
  41.  
  42.     if (glcGetCurrentContext() != contextID) {
  43.     printf("Died at glcGetCurrentContext\n");
  44.     exit(1);
  45.     }
  46.  
  47.     glcPrependCatalog("/usr/lib/X11/fonts/100dpi/fonts.dir");
  48.     if (glcGetError() != GL_NO_ERROR) {
  49.     printf("Died at glcPrependCatalog\n");
  50.     exit(1);
  51.     }
  52.  
  53.     glcAppendCatalog("/usr/lib/X11/fonts/75dpi/fonts.dir");
  54.     if (glcGetError() != GL_NO_ERROR) {
  55.     printf("Died at glcAppendCatalog\n");
  56.     exit(1);
  57.     }
  58.  
  59.     glcRemoveCatalog(2);
  60.     if (glcGetError() != GL_NO_ERROR) {
  61.     printf("Died at glcRemoveCatalog\n");
  62.     exit(1);
  63.     }
  64.  
  65.     glcRemoveCatalog(0);
  66.     if (glcGetError() != GL_NO_ERROR) {
  67.     printf("Died at glcRemoveCatalog\n");
  68.     exit(1);
  69.     }
  70.  
  71.     fontID = glcGenFontID();
  72.     if (glcGetError() != GL_NO_ERROR) {
  73.     printf("Died at glcGenFontID\n");
  74.     exit(1);
  75.     }
  76.  
  77.     if (glcNewFontFromFamily(fontID, "Courier") != fontID) {
  78.     printf("Died at glcNewFontFromFamily\n");
  79.     exit(1);
  80.     }
  81.     faceIndex = 0;
  82.     faceTotal = glcGetFonti(fontID, GLC_FACE_COUNT);
  83.  
  84.     totalFont = glcGeti(GLC_MASTER_COUNT);
  85.     if (glcGetError() != GL_NO_ERROR) {
  86.     printf("Died at glcGeti\n");
  87.     exit(1);
  88.     }
  89.     if (totalFont > 100) {
  90.     totalFont = 100;
  91.     }
  92.  
  93.     index = 0;
  94.     for (i = 0; i < totalFont; i++) {
  95.     printf("%s\n", glcGetMasterc(i, GLC_FAMILY));
  96.     fontIDs[index] = glcGenFontID();
  97.     if (glcNewFontFromMaster(fontIDs[index], i) != fontIDs[index]) {
  98.         printf("Died at glcNewFontFromMaster\n");
  99.         exit(1);
  100.     }
  101.     facesIndex[index] = 0;
  102.     facesTotal[index] = glcGetFonti(fontIDs[index], GLC_FACE_COUNT);
  103.     index++;
  104.     }
  105.  
  106.     totalFont--;
  107.  
  108.     fontIndex = 0;
  109.     charIndex = 0;
  110.  
  111.     glClearColor(0.2, 0.2, 0.2, 0.0);
  112. }
  113.  
  114. void Reshape(int width, int height)
  115. {
  116.  
  117.     glViewport(0, 0, width, height);
  118.  
  119.     glMatrixMode(GL_PROJECTION);
  120.     glLoadIdentity();
  121.     glOrtho(0.0, (double)width, 0.0, (double)height, -10000.0, 10000.0);
  122.     glMatrixMode(GL_MODELVIEW);
  123. }
  124.  
  125. void Key(unsigned char key, int x, int y)
  126. {
  127.     int i, j;
  128.     const char *face;
  129.  
  130.     switch (key) {
  131.     case 'a':
  132.         rotY -= 1.0;
  133.         if (drawStyle == GLC_BITMAP) {
  134.         glcDeleteGLObjects();
  135.         }
  136.         glutPostRedisplay();
  137.         break;
  138.     case 'd':
  139.         rotY += 1.0;
  140.         if (drawStyle == GLC_BITMAP) {
  141.         glcDeleteGLObjects();
  142.         }
  143.         glutPostRedisplay();
  144.         break;
  145.     case 'w':
  146.         rotX -= 1.0;
  147.         if (drawStyle == GLC_BITMAP) {
  148.         glcDeleteGLObjects();
  149.         }
  150.         glutPostRedisplay();
  151.         break;
  152.     case 'x':
  153.         rotX += 1.0;
  154.         if (drawStyle == GLC_BITMAP) {
  155.         glcDeleteGLObjects();
  156.         }
  157.         glutPostRedisplay();
  158.         break;
  159.     case 'q':
  160.         rotZ -= 1.0;
  161.         if (drawStyle == GLC_BITMAP) {
  162.         glcDeleteGLObjects();
  163.         }
  164.         glutPostRedisplay();
  165.         break;
  166.     case 'z':
  167.         rotZ += 1.0;
  168.         if (drawStyle == GLC_BITMAP) {
  169.         glcDeleteGLObjects();
  170.         }
  171.         glutPostRedisplay();
  172.         break;
  173.     case '=':
  174.         scale += 1.0;
  175.         if (drawStyle == GLC_BITMAP) {
  176.         glcDeleteGLObjects();
  177.         }
  178.         glutPostRedisplay();
  179.         break;
  180.     case '-':
  181.         scale -= 1.0;
  182.         if (drawStyle == GLC_BITMAP) {
  183.         glcDeleteGLObjects();
  184.         }
  185.         glutPostRedisplay();
  186.         break;
  187.     case '1':
  188.         dl = (dl == GL_FALSE) ? GL_TRUE : GL_FALSE;
  189.         if (dl == GL_TRUE) {
  190.         glcEnable(GLC_GL_OBJECTS);
  191.         } else {
  192.         glcDisable(GLC_GL_OBJECTS);
  193.         glcDeleteGLObjects();
  194.         }
  195.         glutPostRedisplay();
  196.         break;
  197.     case '2':
  198.         if (drawStyle == GLC_BITMAP) {
  199.         drawStyle = GLC_LINE;
  200.         } else if (drawStyle == GLC_LINE) {
  201.         drawStyle = GLC_TRIANGLE;
  202.         } else if (drawStyle == GLC_TRIANGLE) {
  203.         drawStyle = GLC_BITMAP;
  204.         }
  205.         glcRenderStyle(drawStyle);
  206.         glcDeleteGLObjects();
  207.         glutPostRedisplay();
  208.         break;
  209.     case '3':
  210.         if (++facesIndex[fontIndex] == facesTotal[fontIndex]) {
  211.         facesIndex[fontIndex] = 0;
  212.         }
  213.         face = glcGetFontListc(fontIDs[fontIndex], GLC_FACE_LIST,
  214.                        facesIndex[fontIndex]);
  215.         glcFontFace(0, face);
  216.         glutPostRedisplay();
  217.         break;
  218.     case '4':
  219.         if (++faceIndex == faceTotal) {
  220.         faceIndex = 0;
  221.         }
  222.         face = glcGetFontListc(fontID, GLC_FACE_LIST, faceIndex);
  223.         glcFontFace(fontID, face);
  224.         glutPostRedisplay();
  225.         break;
  226.     case '5':
  227.         if (glcIsEnabled(GLC_AUTO_FONT) == GL_TRUE) {
  228.         glcDisable(GLC_AUTO_FONT);
  229.         } else {
  230.         glcEnable(GLC_AUTO_FONT);
  231.         }
  232.         glutPostRedisplay();
  233.         break;
  234.     case '7':
  235.         if (--charIndex < 0) {
  236.         charIndex = 26 - 1;
  237.         }
  238.         glutPostRedisplay();
  239.         break;
  240.     case '8':
  241.         if (++charIndex == 26) {
  242.         charIndex = 0;
  243.         }
  244.         glutPostRedisplay();
  245.         break;
  246.     case '9':
  247.         if (--fontIndex < 0) {
  248.         fontIndex = totalFont - 1;
  249.         }
  250.         glutPostRedisplay();
  251.         break;
  252.     case '0':
  253.         if (++fontIndex == totalFont) {
  254.         fontIndex = 0;
  255.         }
  256.         glutPostRedisplay();
  257.         break;
  258.     case 27:
  259.         glcDeleteFont(fontID);
  260.         glcDeleteContext(contextID);
  261.         exit(1);
  262.     }
  263. }
  264.  
  265. void Display(void)
  266. {
  267.     GLfloat v[8];
  268.     GLubyte *ptr, str[128], buf[40];
  269.  
  270.     glClear(GL_COLOR_BUFFER_BIT);
  271.  
  272.     if (dl == GL_TRUE) {
  273.     strcpy(str, "Display list, ");
  274.     } else {
  275.     strcpy(str, "Immediate, ");
  276.     }
  277.     if (drawStyle == GLC_BITMAP) {
  278.     strcat(str, "Bitmap, ");
  279.     } else if (drawStyle == GLC_LINE) {
  280.     strcat(str, "Outline, ");
  281.     } else if (drawStyle == GLC_TRIANGLE) {
  282.     strcat(str, "Filled, ");
  283.     }
  284.     strcat(str, glcGetFontFace(fontIDs[fontIndex]));
  285.     sprintf(buf, " (%d/%d), ", facesIndex[fontIndex]+1, facesTotal[fontIndex]);
  286.     strcat(str, buf);
  287.     strcat(str, glcGetFontc(fontIDs[fontIndex], GLC_FAMILY));
  288.  
  289.     glcFont(fontID);
  290.     if (drawStyle == GLC_BITMAP) {
  291.     glcLoadIdentity();
  292.     glcScale(20.0, 20.0);
  293.     glColor3f(1.0, 1.0, 0.0);
  294.     glRasterPos3f(5.0, 20.0, 0.0);
  295.     glcRenderString(str);
  296.     } else {
  297.     glPushMatrix();
  298.     glTranslatef(5.0, 20.0, 0.0);
  299.     glScalef(20.0, 20.0, 1.0);
  300.     glColor3f(1.0, 1.0, 0.0);
  301.     glcRenderString(str);
  302.     glPopMatrix();
  303.     }
  304.  
  305.     glcFont(fontIDs[fontIndex]);
  306.     if (drawStyle == GLC_BITMAP) {
  307.     glcLoadIdentity();
  308.     glcRotate(rotZ);
  309.     glcScale(scale, scale);
  310.     glColor3f(1.0, 0.0, 0.0);
  311.     glRasterPos3f(10.0, 100.0, 0.0);
  312.     glcRenderString("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
  313.     glColor3f(1.0, 0.0, 0.0);
  314.     glRasterPos3f(10.0, 200.0, 0.0);
  315.     glcRenderString("abcdefghijklmnopqrstuvwxyz");
  316.     glPushMatrix();
  317.     glTranslatef(10.0, 200.0, 0.0);
  318.     if (glcMeasureString(GL_TRUE, "abcdefghijklmnopqrstuvwxyz") != 0) {
  319.         glColor3f(0.0, 1.0, 1.0);
  320.         glcGetStringCharMetric(charIndex, GLC_BOUNDS, v);
  321.         glBegin(GL_LINE_STRIP);
  322.         glVertex3f(v[0], v[1], 0.0);
  323.         glVertex3f(v[2], v[3], 0.0);
  324.         glVertex3f(v[4], v[5], 0.0);
  325.         glVertex3f(v[6], v[7], 0.0);
  326.         glVertex3f(v[0], v[1], 0.0);
  327.         glEnd();
  328.     }
  329.     glPopMatrix();
  330.     glColor3f(1.0, 0.0, 0.0);
  331.     glRasterPos3f(10.0, 300.0, 0.0);
  332.     glcRenderString("0123456789");
  333.     glPushMatrix();
  334.     glTranslatef(10.0, 300.0, 0.0);
  335.     if (glcMeasureString(GL_TRUE, "0123456789") != 0) {
  336.         glColor3f(0.0, 1.0, 1.0);
  337.         glcGetStringMetric(GLC_BOUNDS, v);
  338.         glBegin(GL_LINE_STRIP);
  339.         glVertex3f(v[0], v[1], 0.0);
  340.         glVertex3f(v[2], v[3], 0.0);
  341.         glVertex3f(v[4], v[5], 0.0);
  342.         glVertex3f(v[6], v[7], 0.0);
  343.         glVertex3f(v[0], v[1], 0.0);
  344.         glEnd();
  345.     }
  346.     glPopMatrix();
  347.     } else {
  348.     glPushMatrix();
  349.     glRotatef(rotX, 1.0, 0.0, 0.0);
  350.     glRotatef(rotY, 0.0, 1.0, 0.0);
  351.     glRotatef(rotZ, 0.0, 0.0, 1.0);
  352.     glPushMatrix();
  353.     glTranslatef(10.0, 100.0, 0.0);
  354.     glScalef(scale, scale, 1.0);
  355.     if (drawStyle == GLC_LINE) {
  356.         glColor3f(0.0, 1.0, 0.0);
  357.     } else if (drawStyle == GLC_TRIANGLE) {
  358.         glColor3f(0.0, 0.0, 1.0);
  359.     }
  360.     glcRenderString("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
  361.     glPopMatrix();
  362.     glPushMatrix();
  363.     glTranslatef(10.0, 200.0, 0.0);
  364.     glScalef(scale, scale, 1.0);
  365.     if (drawStyle == GLC_LINE) {
  366.         glColor3f(0.0, 1.0, 0.0);
  367.     } else if (drawStyle == GLC_TRIANGLE) {
  368.         glColor3f(0.0, 0.0, 1.0);
  369.     }
  370.     glcRenderString("abcdefghijklmnopqrstuvwxyz");
  371.     glPopMatrix();
  372.     glPushMatrix();
  373.     glTranslatef(10.0, 200.0, 0.0);
  374.     glScalef(scale, scale, 1.0);
  375.     if (glcMeasureString(GL_TRUE, "abcdefghijklmnopqrstuvwxyz") != 0) {
  376.         glcGetStringCharMetric(charIndex, GLC_BOUNDS, v);
  377.         glColor3f(0.0, 1.0, 1.0);
  378.         glBegin(GL_LINE_STRIP);
  379.         glVertex3f(v[0], v[1], 0.0);
  380.         glVertex3f(v[2], v[3], 0.0);
  381.         glVertex3f(v[4], v[5], 0.0);
  382.         glVertex3f(v[6], v[7], 0.0);
  383.         glVertex3f(v[0], v[1], 0.0);
  384.         glEnd();
  385.     }
  386.     glPopMatrix();
  387.     glPushMatrix();
  388.     glTranslatef(10.0, 300.0, 0.0);
  389.     glScalef(scale, scale, 1.0);
  390.     if (drawStyle == GLC_LINE) {
  391.         glColor3f(0.0, 1.0, 0.0);
  392.     } else if (drawStyle == GLC_TRIANGLE) {
  393.         glColor3f(0.0, 0.0, 1.0);
  394.     }
  395.     glcRenderString("0123456789");
  396.     glPopMatrix();
  397.     glPushMatrix();
  398.     glTranslatef(10.0, 300.0, 0.0);
  399.     glScalef(scale, scale, 1.0);
  400.     if (glcMeasureString(GL_TRUE, "0123456789") != 0) {
  401.         glcGetStringMetric(GLC_BOUNDS, v);
  402.         glColor3f(0.0, 1.0, 1.0);
  403.         glBegin(GL_LINE_STRIP);
  404.         glVertex3f(v[0], v[1], 0.0);
  405.         glVertex3f(v[2], v[3], 0.0);
  406.         glVertex3f(v[4], v[5], 0.0);
  407.         glVertex3f(v[6], v[7], 0.0);
  408.         glVertex3f(v[0], v[1], 0.0);
  409.         glEnd();
  410.     }
  411.     glPopMatrix();
  412.     glPopMatrix();
  413.     }
  414.  
  415.     if (glcGetError() != GL_NO_ERROR) {
  416.     printf("Got a GLC error\n");
  417.     }
  418.  
  419.     glutSwapBuffers();
  420. }
  421.  
  422. void main(int argc, char **argv)
  423. {
  424.  
  425.     glutInitWindowSize(700, 500);
  426.     glutInit(&argc, argv);
  427.     glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE);
  428.     glutCreateWindow("glc test");
  429.     Init();
  430.     glutDisplayFunc(Display);
  431.     glutReshapeFunc(Reshape);
  432.     glutKeyboardFunc(Key);
  433.     glutMainLoop();
  434. }
  435.